<!-- 新建样品里的客户选择组件 -->
<template>
	<div>
		<u-popup safe-area-inset-bottom v-model="show" mode="left">

			<view style="">
				<customNavbar :title="title" @back="show = false" rightText="确定" @clickRight="customerChoiceConfirm">
				</customNavbar>
			</view>

			<!-- 搜索区域 -->
			<div style="position: fixed;width: 99%;background-color: #fff;z-index: 999;">
				<div v-if="!isHidden" style="margin-top: 10px;">
					<uni-search-bar style="color:#fff" :textColor="'#ccc'" @confirm="search"
						v-model="queryParam.Keyword" @clear="cancel" placeholder="请输入客户名称"
						@cancel="cancel"></uni-search-bar>
				</div>
			</div>
			<div class="outerBox" style="width:300px;padding-top:140rpx;">
				<!-- 滑动二级导航栏 -->
				<scroll-view scroll-y="true" class="log-shop-item" :show-scrollbar="true" style="">
					<u-tag v-if="CustomerList.length==0" style="margin-right: 20rpx;margin-bottom: 10rpx;"
						:text="'未选择客户'" type="error" />
					<block v-for="(item, index) in CustomerList" :key="index">
						<u-tag style="margin-right: 20rpx;margin-bottom: 10rpx;" :text="item.cCusName" type="primary"
							closeable @close="tagCloseChange(item, index)" />
					</block>
				</scroll-view>
				<scroll-view style="width: 100%;height: 100%;" scroll-y="true" @scrolltolower="lower">
					<div class="ckbxcli" @click="customerChoice(item)" v-for="(item, index) in data" :key="index">
						<div style="text-align: left;">
							<view style="display: flex;justify-content: space-between;">
								<view span="20">
									<span style="font-size: 14px;font-weight: bold">{{ item.cCusName }}</span>
								</view>
							</view>
							<u-row style="">
								<u-col>
									<p style="">客户编码：<span>{{ item.cCusCode }}</span></p>
								</u-col>
							</u-row>
							<u-row style="">
								<u-col span="11">
									<p style="">备案人：<span>{{ item.RealName }}</span></p>
								</u-col>
								<u-col span="13">
									<view style="">备案时间： <uni-dateformat
											:date="$moment(item.CreateTime).format('YYYY-MM-DD hh:mm:ss') "></uni-dateformat>
									</view>
								</u-col>
							</u-row>

						</div>
					</div>

					<div style="padding-bottom: 20px;" v-if="data.length>0">
						<LoadingMore :status="status" :contentText="contentText" :rows="data"></LoadingMore>
					</div>

					<div v-if="isSearchUser" class="search-none">
						<img src="../assets/search.png" alt="">
						<h4 style="color:#6C6C6C;margin-top:5px">客户检索</h4>
					</div>
				</scroll-view>
			</div>
		</u-popup>
	</div>
</template>

<script>
	// import PaginationCom from "@/components/PaginationCom.vue";
	import {
		// 用于报销专用选择客户
		CustomerInfoGetUionDataList
	} from '@/api/interface.js'
	export default {
		props: {
			// 弹窗标题
			title: {
				type: String,
				default: '选择客户'
			},
			// 是否隐藏客户选择搜索栏
			isHidden: {
				type: Boolean,
				default: false,
			},
			isAllData: {
				default: false,
				type: Boolean
			} // 是否查询全部数据
		},
		components: {
			// PaginationCom,
			// "van-pagination": Pagination,
		},
		data() {
			return {
				marginTop: 0,
				// 默认显示二级导航
				tabbarIndex: 0,
				//二级 导航数据
				tabbarListData: [],
				// 加载状态
				status: "loading",
				contentText: {
					contentdown: '轻轻上拉',
					contentrefresh: '努力加载中...',
					contentnomore: ''
				},
				immediateCheck: false,
				loading: false,
				finished: false,
				selectedRowKeys: [],
				queryParam: {
					Condition: "cCusName",
					Keyword: ""
					// cCusName: "",
					// cCusType: null,
					// isAllData: this.isAllData
				},
				pagination: {
					current: 1,
					pageSize: 10,
					Total: 0,
				},
				entity: {},
				judgment: true,
				data: [],
				show: false,
				Id: "",
				PageRows: 20,
				isSearchUser: false,
				// 搜索出来的客户列表
				CustomerList: [],
				// 传递过来的选中的客户数组
				CustomerNameArr: [],
				CustomIdArr: []
			};
		},
		onLoad() {
			// 组件创建的时候初始默认检索提示
			this.isSearchUser = true
		},
		methods: {
			// 选择客户标签阿牛关闭出发
			tagCloseChange(item, index) {

				this.CustomerList.splice(index, 1)
				console.log('关闭了', item, index, this.CustomerList)
				this.$emit('tagCloseChange', index)

			},
			// 触底
			lower(e) {
				if (this.data.length >= this.pagination.Total) return
				// 上拉加载
				this.pagination.current++;
				this.getDataList()
			},
			onLoading() {
				this.loading = true; //防止第一页重复加载
				// 获取客户列表
				this.getDataList();
			},
			paginationchange(a) {
				// a:当前选中页码
				this.pagination.current = a;
				this.getDataList();
			},
			//返回
			onClickLeft() {
				this.show = false;
				// 返回时候显示检索提示
				this.isSearchUser = true
				//列表 数据重置
				this.queryParam.Keyword = ""
				this.data = []
			},
			close() {
				this.show = false;
			},
			// 取消
			cancel() {
				this.queryParam.Keyword = ""
				this.data = [];
				this.status = "noMore"
				this.isSearchUser = true
			},
			onSearch() {
				if (this.queryParam.Keyword) {
					this.search(this.queryParam.Keyword)
				} else {
					this.search
				}

			},
			//搜索
			search(value) {
				this.isSearchUser = false
				this.pagination.current = 1
				this.data = []
				this.status = "loading"
				this.getDataList();
			},

			// 选择客户确定后
			customerChoice(item) {
				console.log('先选择客户好了------', item, this.CustomerList, this.data);
				// 第一种去重  暂时不要删除
				// 数组去重
				// this.CustomerList = Array.from(new Set(this.CustomerList.map(JSON.stringify))).map(JSON.parse);
				// 暂时不删除
				// let objects = {
				// 	name: item.cCusName,
				// 	value: item.Id
				// }
				this.CustomerList.push(item)
				// 第二种去重  过滤不同id  想通名称的数组
				let newCustomerList = this.$utils.uniqueMultiPropertyArray(this.CustomerList)
				this.CustomerList = newCustomerList


			},
			// 选择客户确定后
			customerChoiceConfirm() {
				// 判断是否有选择客户
				if (this.CustomerList.length == 0) {
					this.$errorToast('请至少选择一个客户')
					return
				}
				this.$emit("customerChoice", this.CustomerList);
				this.show = false;
			},
			// 获取客户列表
			getDataList(type) {
				console.log(this.isAllData)
				this.status = "loading"
				CustomerInfoGetUionDataList({
						PageIndex: this.pagination.current,
						PageRows: this.pagination.pageSize,
						SortField: "cCusCode",
						SortType: "desc",
						Search: this.queryParam,
						// ...this.filters
					})
					.then(resJson => {
						if (resJson.Success) {
							// 赋值总条数
							this.pagination.Total = resJson.Total;
							if (resJson.Data.length == 0 || resJson.Data == null) {
								this.status = "noMore"
								return
							}
							if (this.pagination.current == 1) {
								this.data = resJson.Data
							} else {
								// 有数据 赋值列表 加载状态结束
								this.data = this.data.concat(resJson.Data)
							}



							if (type == 1) {
								//说明是详情过可能有客户数据的  
								// 不让他显示列表 只处理上面的tag表掐数据即可
								this.data = []

								//有可能是多个结果 所有循环对比过滤
								resJson.Data.forEach(item => {
									this.CustomIdArr.forEach(v => {
										if (item.Id === v) {
											this.CustomerList.push(item)
											// 第二种去重  过滤不同id  想通名称的数组
											this.CustomerList = this.$utils.uniqueMultiPropertyArray(
												this.CustomerList)
										}
									})
								})
							} else if (type == 2) {
								// 不让他显示列表 只处理上面的tag表掐数据即可
								this.data = []

								resJson.Data.forEach(item => {
									this.CustomIdArr.forEach(v => {
										if (item.Id === v) {
											this.CustomerList.push(item)
											// 第二种去重  过滤不同id  想通名称的数组
											this.CustomerList = this.$utils.uniqueMultiPropertyArray(
												this.CustomerList)
										}
									})
								})
								console.log('开始处理了typeforEachforEach', type, this.CustomIdArr, this.CustomerList)
								// this.CustomerList.push(resJson.Data)
								// 第二种去重  过滤不同id  想通名称的数组
								// let newCustomerList = this.$utils.uniqueMultiPropertyArray(this.CustomerList)
								// this.CustomerList = newCustomerList
								// console.log('去重后的结果---------', resJson.Data, this.CustomerList)
							}




							// 如果list长度大于等于总数据条数，数据全部加载完成
							if (this.data.length >= resJson.Total) {
								this.status = "noMore"
								return
							}

						} else {
							this.$errorToast(resJson.Msg);
							this.status = "noMore"
						}
					});
			},
			init() {
				this.show = true;
				this.cancel()
			},
			openForm(entity) {
				this.init();
				console.log('传递过来的客户数entity', entity)
				// 用于修改详情
				if (entity) {
					if (entity.CustomId) {
						this.CustomerNameArr = entity.CustomerName.split(',')
						this.CustomIdArr = entity.CustomId.split(',')

						if (this.CustomerNameArr.length == 1) {
							this.queryParam.Keyword = entity.CustomerName
							this.getDataList(1)
						} else {
							console.log('打印过来的选择好的客户myuser里的', entity, this.CustomerNameArr, this.CustomIdArr)
							this.CustomerNameArr.forEach(item => {
								this.queryParam.Keyword = item
								this.getDataList(2)
							})
						}
					}
				}
				this.init();
			}
		}
	};
</script>

<style scoped lang="less">
	/* 覆盖uniapp顶部导航栏左侧文字颜色 */
	::v-deep.uni-navbar {
		.uni-nav-bar-right-text {
			color: #1959FA !important;
		}
	}

	// 滑动导航栏
	.log-tabbar-content {

		//设置滚动列表 **************************************
		.log-tabbar-list {
			.log-shop-item {
				// white-space: nowrap; // 滚动必须加的属性
			}
		}
	}

	.search-none {
		height: 70%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.outerBox {
		/* 	height: calc(100vh - 120px);
		padding-bottom: 50px;
		box-sizing: border-box;
		overflow: auto; */
		/* #ifdef H5 */
		height: calc(100vh - 50px);
		/* #endif */
		/* #ifdef APP */
		height: calc(100vh - 220px);
		/* #endif */
	}

	.ckbxcli {
		border: 1px solid Gainsboro;
		width: 95%;
		text-align: center;
		margin-left: 2%;
		margin-top: 10px;
		box-sizing: border-box;
		padding: 20rpx;
		/* 	padding-right: 0px;
		padding-left: 0px;
		padding-bottom: 2px; */
		cursor: pointer;
		/* padding-top: 6px; */
		list-style-type: none;
		display: inline-block;
	}

	.ul {
		/* margin-top: 20px; */
	}

	.nav {
		position: fixed !important;
		width: 100%;
	}
</style>